'''
每一条二叉树的「直径」长度，就是一个节点的左右子树的最大深度之和
'''


# Definition for a binary tree node.
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
class Solution:
    def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int:
        self.maxdeep = 0
        
        def maxdepth(root):
            if root is None:
                return 0
            
            left = maxdepth(root.left)
            right = maxdepth(root.right)

            mydepth = left + right
            self.maxdeep = max(self.maxdeep,mydepth)
            return 1 + max(left,right)
        
        
        maxdepth(root)
        return self.maxdeep